package com.nanyang.tms.manager.cost.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.cost.*;
import com.nanyang.tms.domain.cost.*;
import com.nanyang.tms.manager.cost.CostManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Component("costManager")
public class CostManagerImpl implements CostManager {
    @Autowired
    private CostInfoDao costInfoDao;
    @Autowired
    private CostPartCityDao costPartCityDao;
    @Autowired
    private CostPartCountyDao costPartCountyDao;
    @Autowired
    private CostPartDao costPartDao;
    @Autowired
    private CostPartSchemeDao costPartSchemeDao;
    @Autowired
    private CostPartZipcodeDao costPartZipcodeDao;
    @Autowired
    private CostPriceRecordDao costPriceRecordDao;
    @Autowired
    private CostWeightDao costWeightDao;
    @Autowired
    private CostWeightRecordDao costWeightRecordDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Override
    public PageBean<CostInfoVO> listCost(CostInfoQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = costInfoDao.countCost(query);
            PageBean<CostInfoVO> page = new PageTableBean<CostInfoVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(costInfoDao.listCost(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemLog(module =SystemLog.Module.COST,template="costAdd")
    @Override
    @Transactional
    public void saveCost(CostBaseVO costBaseVO) {
        try {
            CostInfoVO costInfoVO = costBaseVO.getCostInfoVO();
            CostInfoQuery query=new CostInfoQuery();
            query.setProductCode(costInfoVO.getProductCode());
            if (costInfoDao.queryVerifyCost(query)>0){
                throw new TmsManagerException(TmsExceptionEnum.PRODUCT_IS_EXIST.getCode(),TmsExceptionEnum.PRODUCT_IS_EXIST.getMessage());
            };
            CostInfoDO costInfoDO=new CostInfoDO();//设置成本价格基本信息
            BeanUtils.copyProperties(costBaseVO.getCostInfoVO(),costInfoDO);
            String costCode= TmsSerializeConstant.COST_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            costInfoDO.setCostCode(costCode);
            costInfoDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
            costInfoDO.setIp(costBaseVO.getIp());
            costInfoDao.insert(costInfoDO);

            CostPartSchemeDO costPartSchemeDO=new CostPartSchemeDO();//分区方案
            BeanUtils.copyProperties(costBaseVO.getCostPartSchemeVO(),costPartSchemeDO);
            String partSchemeCode= TmsSerializeConstant.COST_PART_SCHEME_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_SCHEME_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            costPartSchemeDO.setPartSchemeCode(partSchemeCode);
            costPartSchemeDO.setCostCode(costCode);
            costPartSchemeDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
            costPartSchemeDO.setIp(costBaseVO.getIp());
            costPartSchemeDao.insert(costPartSchemeDO);

            List<String> countyList=new ArrayList<>();//国家重复
            List<String> repCountyList=new ArrayList<>();//国家重复

            Map<String,CostPartDO> costPartMap=new HashMap<>();
            List<CostPartVO> costPartVOList = costBaseVO.getCostPartSchemeVO().getCostPartVOList();
            costPartVOList.forEach(costPartVO ->{
                CostPartDO costPartDO=new CostPartDO();//分区列表信息
                BeanUtils.copyProperties(costPartVO,costPartDO);
                String partCode= TmsSerializeConstant.COST_PART_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                costPartDO.setPartCode(partCode);
                costPartDO.setPartSchemeCode(partSchemeCode);
                costPartDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                costPartDO.setIp(costBaseVO.getIp());
                costPartDao.insert(costPartDO);
                costPartMap.put(costPartVO.getPartNo(),costPartDO);
                List<CostPartCountyVO> costPartCountyVOList = costPartVO.getCostPartCountyVOList();
                costPartCountyVOList.forEach(costPartCountyVO ->{
                    CostPartCountyDO costPartCountyDO=new CostPartCountyDO();//分区国家列表信息
                    BeanUtils.copyProperties(costPartCountyVO,costPartCountyDO);
                    String partCountyCode= TmsSerializeConstant.COST_PART_COUNTY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_COUNTY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                    costPartCountyDO.setPartCountyCode(partCountyCode);
                    costPartCountyDO.setPartCode(partCode);
                    costPartCountyDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                    costPartCountyDO.setIp(costBaseVO.getIp());
                    costPartCountyDO.setPartSchemeCode(partSchemeCode);
                    costPartCountyDao.insert(costPartCountyDO);
                    if(countyList.contains(costPartCountyVO.getCountyName())){
                        repCountyList.add(costPartCountyVO.getCountyName());
                    }else{
                        countyList.add(costPartCountyVO.getCountyName());
                    }
                    List<CostPartCityVO> costPartCityVOList = costPartCountyVO.getCostPartCityVOList();
                    if(CollectionUtil.isNotEmpty(costPartCityVOList)){
                        costPartCityVOList.forEach(costPartCityVO ->{
                            CostPartCityDO costPartCityDO=new CostPartCityDO();//分区城市列表信息
                            BeanUtils.copyProperties(costPartCityVO,costPartCityDO);
                            String partCityCode= TmsSerializeConstant.COST_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                            costPartCityDO.setPartCityCode(partCityCode);
                            costPartCityDO.setPartCountyCode(partCountyCode);
                            costPartCityDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                            costPartCityDO.setIp(costBaseVO.getIp());
                            costPartCityDao.insert(costPartCityDO);
                        });
                    }
                    List<CostPartZipcodeVO> costPartZipcodeVOList = costPartCountyVO.getCostPartZipcodeVOList();
                    if(CollectionUtil.isNotEmpty(costPartZipcodeVOList)){
                        costPartZipcodeVOList.forEach(costPartZipcodeVO ->{
                            CostPartZipcodeDO costPartZipcodeDO=new CostPartZipcodeDO();//分区邮编列表信息
                            BeanUtils.copyProperties(costPartZipcodeVO,costPartZipcodeDO);
                            String partZipCode= TmsSerializeConstant.COST_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                            costPartZipcodeDO.setPartZipCode(partZipCode);
                            costPartZipcodeDO.setPartCountyCode(partCountyCode);
                            costPartZipcodeDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                            costPartZipcodeDO.setIp(costBaseVO.getIp());
                            costPartZipcodeDao.insert(costPartZipcodeDO);
                        });
                    }

                });

            });

            if(CollectionUtil.isNotEmpty(repCountyList)){//国家存在重复的
                throw new TmsManagerException(TmsExceptionEnum.COUNTY_IS_EXIST.getCode(),String.format(TmsExceptionEnum.COUNTY_IS_EXIST.getMessage(), JSONUtil.toJsonStr(repCountyList)));
            }


            CostWeightDO costWeightDO=new CostWeightDO();//重量模板信息
            BeanUtils.copyProperties(costBaseVO.getCostWeightVO(),costWeightDO);
            String weightCode= TmsSerializeConstant.COST_WEIGHT_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_WEIGHT_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
            costWeightDO.setWeightCode(weightCode);
            costWeightDO.setCostCode(costCode);
            costWeightDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
            costWeightDO.setIp(costBaseVO.getIp());
            costWeightDao.insert(costWeightDO);

            Map<String,CostWeightRecordDO> costWeightRecordMap=new HashMap<>();
            List<CostWeightRecordVO> costWeightRecordVOList = costBaseVO.getCostWeightVO().getCostWeightRecordVOList();
            costWeightRecordVOList.forEach(costWeightRecordVO ->{
                CostWeightRecordDO costWeightRecordDO=new CostWeightRecordDO();//重量模板明细
                BeanUtils.copyProperties(costWeightRecordVO,costWeightRecordDO);
                String weightRecordCode= TmsSerializeConstant.COST_WEIGHT_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_WEIGHT_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                costWeightRecordDO.setWeightRecordCode(weightRecordCode);
                costWeightRecordDO.setWeightCode(weightCode);
                costWeightRecordDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                costWeightRecordDO.setIp(costBaseVO.getIp());
                costWeightRecordDao.insert(costWeightRecordDO);
                costWeightRecordMap.put(costWeightRecordVO.getWeightRecordNo(),costWeightRecordDO);
            });


            List<CostPriceRecordVO> costPriceRecordVOList = costBaseVO.getCostPriceRecordVOList();
            costPriceRecordVOList.forEach(costPriceRecordVO ->{
                CostPriceRecordDO costPriceRecordDO=new CostPriceRecordDO();//成本价格明细
                String priceRecordCode= TmsSerializeConstant.COST_PRICE_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PRICE_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                costPriceRecordDO.setPrice(costPriceRecordVO.getPrice());
                CostPartDO costPartDO = costPartMap.get(costPriceRecordVO.getPartNo());
                if(ObjectUtil.isNotNull(costPartDO)){
                    costPriceRecordDO.setPartCode(costPartDO.getPartCode());
                    costPriceRecordDO.setPartName(costPartDO.getPartName());
                    costPriceRecordDO.setPartSchemeCode(costPartDO.getPartSchemeCode());
                }
                CostWeightRecordDO costWeightRecordDO = costWeightRecordMap.get(costPriceRecordVO.getWeightRecordNo());
                if(ObjectUtil.isNotNull(costWeightRecordDO)){
                    costPriceRecordDO.setWeightRecordCode(costWeightRecordDO.getWeightRecordCode());
                    costPriceRecordDO.setWeightRecordName(costWeightRecordDO.getWeightRecordName());
                    costPriceRecordDO.setWeightCode(costWeightRecordDO.getWeightCode());
                }
                costPriceRecordDO.setCostRecordCode(priceRecordCode);
                costPriceRecordDO.setCostCode(costCode);
                costPriceRecordDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                costPriceRecordDO.setIp(costBaseVO.getIp());
                costPriceRecordDao.insert(costPriceRecordDO);
            });
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @Override
    public CostBaseVO selectByCode(String costCode){
        try {
            CostBaseVO costBaseVO=new CostBaseVO();
            CostInfoVO costInfoVO = costInfoDao.selectByCode(costCode);
            if(Objects.isNull(costInfoVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            costBaseVO.setCostInfoVO(costInfoVO);
            CostPartSchemeVO costPartSchemeVO = costPartSchemeDao.selectByCode(costCode);
            if(Objects.isNull(costPartSchemeVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }

            List<CostPartVO> costPartVOS = costPartDao.selectByCode(costPartSchemeVO.getPartSchemeCode());
            if(CollectionUtil.isEmpty(costPartVOS)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            costPartVOS.forEach(costPartVO ->{
                List<CostPartCountyVO> costPartCountyVOS = costPartCountyDao.selectByCode(costPartVO.getPartCode());
                if(CollectionUtil.isEmpty(costPartCountyVOS)){
                    throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
                }
                costPartCountyVOS.forEach(costPartCountyVO ->{
                    List<CostPartCityVO> costPartCityVOS = costPartCityDao.selectByCode(costPartCountyVO.getPartCountyCode());
                    List<CostPartZipcodeVO> costPartZipcodeVOS = costPartZipcodeDao.selectByCode(costPartCountyVO.getPartCountyCode());
                    costPartCountyVO.setCostPartCityVOList(costPartCityVOS);
                    costPartCountyVO.setCostPartZipcodeVOList(costPartZipcodeVOS);
                });
                costPartVO.setCostPartCountyVOList(costPartCountyVOS);
            });
            costPartSchemeVO.setCostPartVOList(costPartVOS);
            costBaseVO.setCostPartSchemeVO(costPartSchemeVO);

            CostWeightVO costWeightVO = costWeightDao.selectByCode(costCode);
            if(Objects.isNull(costWeightVO)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            List<CostWeightRecordVO> costWeightRecordVOS = costWeightRecordDao.selectByCode(costWeightVO.getWeightCode());
            if(CollectionUtil.isEmpty(costWeightRecordVOS)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            costWeightVO.setCostWeightRecordVOList(costWeightRecordVOS);
            costBaseVO.setCostWeightVO(costWeightVO);

            List<CostPriceRecordVO> costPriceRecordVOS = costPriceRecordDao.selectByCode(costCode);
            if(CollectionUtil.isEmpty(costPriceRecordVOS)){
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            costBaseVO.setCostPriceRecordVOList(costPriceRecordVOS);

            return costBaseVO;
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemLog(module =SystemLog.Module.COST,template="costUpdate")
    @Override
    @Transactional
    public  void updateCost(CostBaseVO costBaseVO){
        try {
            CostInfoVO costInfoVO = costBaseVO.getCostInfoVO();
            CostInfoQuery query=new CostInfoQuery();
            query.setProductCode(costInfoVO.getProductCode());
            query.setCargoTypeCode(costInfoVO.getCargoTypeCode());
            query.setCostCode(costInfoVO.getCostCode());
            if (costInfoDao.queryVerifyCost(query)>0){
                throw new TmsManagerException(TmsExceptionEnum.PRODUCT_IS_EXIST.getCode(),TmsExceptionEnum.PRODUCT_IS_EXIST.getMessage());
            };
            Date date = new Date();
            CostInfoDO costInfoDO=new CostInfoDO();
            BeanUtils.copyProperties(costInfoVO,costInfoDO);
            costInfoDO.setGmtModified(date);
            costInfoDO.setGmtModifiedPin(costBaseVO.getGmtModifiedPin());
            costInfoDao.update(costInfoDO);//修改成本基本信息

            CostPartSchemeVO costPartSchemeVO = costBaseVO.getCostPartSchemeVO();
            CostPartSchemeDO  costPartSchemeDO=new CostPartSchemeDO();
            BeanUtils.copyProperties(costPartSchemeVO,costPartSchemeDO);
            costPartSchemeDO.setGmtModified(date);
            costPartSchemeDO.setGmtModifiedPin(costBaseVO.getGmtModifiedPin());
            costPartSchemeDao.update(costPartSchemeDO);//修改分区方案信息

            List<CostPartVO> costPartVOList = costPartSchemeVO.getCostPartVOList();
            List<CostPartVO> costPartVOS = costPartDao.selectByCode(costPartSchemeVO.getPartSchemeCode());
            costPartVOS.forEach(costPartVO ->{
                Optional<CostPartVO> costPartOptional = costPartVOList.stream().filter(item -> costPartVO.getPartCode().equals(item.getPartCode())).findFirst();
                if(!costPartOptional.isPresent()){//删除不存在的
                    List<CostPartCountyVO> costPartCountyVOS = costPartCountyDao.selectByCode(costPartVO.getPartCode());
                    costPartCountyVOS.forEach(costPartCountyVO ->{
                        costPartCityDao.deleteByCountyCode(costPartCountyVO.getPartCountyCode());//删除分区城市信息
                        costPartZipcodeDao.deleteByCountyCode(costPartCountyVO.getPartCountyCode());//删除分区邮编信息
                    });
                    costPartDao.deleteByPrimaryKey(costPartVO.getId());//删除分区信息
                    costPartCountyDao.deleteByPartCode(costPartVO.getPartCode());//删除分区国家信息
                }
            });

            List<String> countyList=new ArrayList<>();//国家重复
            List<String> repCountyList=new ArrayList<>();//国家重复
            Map<String,CostPartDO> costPartMap=new HashMap<>();

            costPartVOList.forEach(costPartVO ->{
                if(Objects.isNull(costPartVO.getPartCode())){//编码不存在新增
                    CostPartDO costPartDO=new CostPartDO();
                    BeanUtils.copyProperties(costPartVO,costPartDO);
                    String partCode= TmsSerializeConstant.COST_PART_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                    costPartDO.setPartCode(partCode);
                    costPartDO.setPartSchemeCode(costPartSchemeDO.getPartSchemeCode());
                    costPartDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                    costPartDO.setIp(costBaseVO.getIp());
                    costPartDao.insert(costPartDO);//分区列表信息
                    costPartMap.put(costPartVO.getPartNo(),costPartDO);
                    List<CostPartCountyVO> costPartCountyVOList = costPartVO.getCostPartCountyVOList();
                    costPartCountyVOList.forEach(costPartCountyVO ->{
                        CostPartCountyDO costPartCountyDO=new CostPartCountyDO();
                        BeanUtils.copyProperties(costPartCountyVO,costPartCountyDO);
                        String partCountyCode= TmsSerializeConstant.COST_PART_COUNTY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_COUNTY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                        costPartCountyDO.setPartCountyCode(partCountyCode);
                        costPartCountyDO.setPartCode(partCode);
                        costPartCountyDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                        costPartCountyDO.setIp(costBaseVO.getIp());
                        costPartCountyDO.setPartSchemeCode(costPartSchemeDO.getPartSchemeCode());
                        costPartCountyDao.insert(costPartCountyDO);//分区国家列表信息
                        if(countyList.contains(costPartCountyVO.getCountyName())){
                            repCountyList.add(costPartCountyVO.getCountyName());
                        }else{
                            countyList.add(costPartCountyVO.getCountyName());
                        }
                        List<CostPartCityVO> costPartCityVOList = costPartCountyVO.getCostPartCityVOList();
                        if(CollectionUtil.isNotEmpty(costPartCityVOList)){
                            costPartCityVOList.forEach(costPartCityVO ->{
                                CostPartCityDO costPartCityDO=new CostPartCityDO();
                                BeanUtils.copyProperties(costPartCityVO,costPartCityDO);
                                String partCityCode= TmsSerializeConstant.COST_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                costPartCityDO.setPartCityCode(partCityCode);
                                costPartCityDO.setPartCountyCode(partCountyCode);
                                costPartCityDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                                costPartCityDO.setIp(costBaseVO.getIp());
                                costPartCityDao.insert(costPartCityDO);//分区城市列表信息
                            });
                        }
                        List<CostPartZipcodeVO> costPartZipcodeVOList = costPartCountyVO.getCostPartZipcodeVOList();
                        if(CollectionUtil.isNotEmpty(costPartZipcodeVOList)){
                            costPartZipcodeVOList.forEach(costPartZipcodeVO ->{
                                CostPartZipcodeDO costPartZipcodeDO=new CostPartZipcodeDO();
                                BeanUtils.copyProperties(costPartZipcodeVO,costPartZipcodeDO);
                                String partZipCode= TmsSerializeConstant.COST_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                costPartZipcodeDO.setPartZipCode(partZipCode);
                                costPartZipcodeDO.setPartCountyCode(partCountyCode);
                                costPartZipcodeDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                                costPartZipcodeDO.setIp(costBaseVO.getIp());
                                costPartZipcodeDao.insert(costPartZipcodeDO);//分区邮编列表信息
                            });
                        }

                    });
                }else{
                    CostPartDO costPartDO=new CostPartDO();
                    BeanUtils.copyProperties(costPartVO,costPartDO);
                    costPartDO.setGmtModified(date);
                    costPartDO.setGmtModifiedPin(costBaseVO.getGmtModifiedPin());
                    costPartDO.setPartSchemeCode(costPartSchemeDO.getPartSchemeCode());
                    costPartDao.update(costPartDO);//分区信息
                    costPartMap.put(costPartVO.getPartNo(),costPartDO);

                    List<CostPartCountyVO> costPartCountyVOList = costPartVO.getCostPartCountyVOList();
                    List<CostPartCountyVO> costPartCountyVOS = costPartCountyDao.selectByCode(costPartVO.getPartCode());
                    costPartCountyVOS.forEach(costPartCountyVO ->{
                        Optional<CostPartCountyVO> costPartCountyOptional = costPartCountyVOList.stream().filter(item -> costPartCountyVO.getPartCountyCode().equals(item.getPartCountyCode())).findFirst();
                        if(!costPartCountyOptional.isPresent()){//删除不存在的
                            costPartCountyDao.deleteByPrimaryKey(costPartCountyVO.getId());//删除分区国家信息
                            costPartCityDao.deleteByCountyCode(costPartCountyVO.getCountyCode());//删除分区城市信息
                            costPartZipcodeDao.deleteByCountyCode(costPartCountyVO.getCountyCode());//删除分区邮编信息
                        }
                    });
                    costPartCountyVOList.forEach(costPartCountyVO ->{
                        if(ObjectUtil.isNull(costPartCountyVO.getPartCountyCode())){//编码不存在新增
                            CostPartCountyDO costPartCountyDO=new CostPartCountyDO();
                            BeanUtils.copyProperties(costPartCountyVO,costPartCountyDO);
                            String partCountyCode= TmsSerializeConstant.COST_PART_COUNTY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_COUNTY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                            costPartCountyDO.setPartCountyCode(partCountyCode);
                            costPartCountyDO.setPartCode(costPartVO.getPartCode());
                            costPartCountyDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                            costPartCountyDO.setIp(costBaseVO.getIp());
                            costPartCountyDO.setPartSchemeCode(costPartSchemeDO.getPartSchemeCode());
                            costPartCountyDao.insert(costPartCountyDO);//分区国家列表信息
                            if(countyList.contains(costPartCountyVO.getCountyName())){
                                repCountyList.add(costPartCountyVO.getCountyName());
                            }else{
                                countyList.add(costPartCountyVO.getCountyName());
                            }
                            List<CostPartCityVO> costPartCityVOList = costPartCountyVO.getCostPartCityVOList();
                            if(CollectionUtil.isNotEmpty(costPartCityVOList)){
                                costPartCityVOList.forEach(costPartCityVO ->{
                                    CostPartCityDO costPartCityDO=new CostPartCityDO();
                                    BeanUtils.copyProperties(costPartCityVO,costPartCityDO);
                                    String partCityCode= TmsSerializeConstant.COST_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    costPartCityDO.setPartCityCode(partCityCode);
                                    costPartCityDO.setPartCountyCode(partCountyCode);
                                    costPartCityDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                                    costPartCityDO.setIp(costBaseVO.getIp());
                                    costPartCityDao.insert(costPartCityDO);//分区城市列表信息
                                });
                            }
                            List<CostPartZipcodeVO> costPartZipcodeVOList = costPartCountyVO.getCostPartZipcodeVOList();
                            if(CollectionUtil.isNotEmpty(costPartZipcodeVOList)){
                                costPartZipcodeVOList.forEach(costPartZipcodeVO ->{
                                    CostPartZipcodeDO costPartZipcodeDO=new CostPartZipcodeDO();
                                    BeanUtils.copyProperties(costPartZipcodeVO,costPartZipcodeDO);
                                    String partZipCode= TmsSerializeConstant.COST_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    costPartZipcodeDO.setPartZipCode(partZipCode);
                                    costPartZipcodeDO.setPartCountyCode(partCountyCode);
                                    costPartZipcodeDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                                    costPartZipcodeDO.setIp(costBaseVO.getIp());
                                    costPartZipcodeDao.insert(costPartZipcodeDO);//分区邮编列表信息
                                });
                            }
                        }else{
                            CostPartCountyDO costPartCountyDO=new CostPartCountyDO();//分区国家列表信息
                            BeanUtils.copyProperties(costPartCountyVO,costPartCountyDO);
                            costPartCountyDO.setGmtModified(date);
                            costPartCountyDO.setGmtModifiedPin(costBaseVO.getGmtModifiedPin());
                            costPartCountyDao.update(costPartCountyDO);
                            if(countyList.contains(costPartCountyVO.getCountyName())){
                                repCountyList.add(costPartCountyVO.getCountyName());
                            }else{
                                countyList.add(costPartCountyVO.getCountyName());
                            }
                            costPartCityDao.deleteByCountyCode(costPartCountyVO.getPartCountyCode());//删除分区城市信息
                            costPartZipcodeDao.deleteByCountyCode(costPartCountyVO.getPartCountyCode());//删除分区邮编信息
                            List<CostPartCityVO> costPartCityVOList = costPartCountyVO.getCostPartCityVOList();
                            if(CollectionUtil.isNotEmpty(costPartCityVOList)){
                                costPartCityVOList.forEach(costPartCityVO ->{
                                    CostPartCityDO costPartCityDO=new CostPartCityDO();//分区城市列表信息
                                    BeanUtils.copyProperties(costPartCityVO,costPartCityDO);
                                    String partCityCode= TmsSerializeConstant.COST_PART_CITY_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_CITY_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    costPartCityDO.setPartCityCode(partCityCode);
                                    costPartCityDO.setPartCountyCode(costPartCountyVO.getPartCountyCode());
                                    costPartCityDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                                    costPartCityDO.setIp(costBaseVO.getIp());
                                    costPartCityDao.insert(costPartCityDO);
                                });
                            }
                            List<CostPartZipcodeVO> costPartZipcodeVOList = costPartCountyVO.getCostPartZipcodeVOList();
                            if(CollectionUtil.isNotEmpty(costPartZipcodeVOList)){
                                costPartZipcodeVOList.forEach(costPartZipcodeVO ->{
                                    CostPartZipcodeDO costPartZipcodeDO=new CostPartZipcodeDO();//分区邮编列表信息
                                    BeanUtils.copyProperties(costPartZipcodeVO,costPartZipcodeDO);
                                    String partZipCode= TmsSerializeConstant.COST_PART_ZIPCODE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PART_ZIPCODE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                                    costPartZipcodeDO.setPartZipCode(partZipCode);
                                    costPartZipcodeDO.setPartCountyCode(costPartCountyVO.getPartCountyCode());
                                    costPartZipcodeDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                                    costPartZipcodeDO.setIp(costBaseVO.getIp());
                                    costPartZipcodeDao.insert(costPartZipcodeDO);
                                });
                            }
                        }
                    });

                }

            });

            if(CollectionUtil.isNotEmpty(repCountyList)){//国家存在重复的
                throw new TmsManagerException(TmsExceptionEnum.COUNTY_IS_EXIST.getCode(),String.format(TmsExceptionEnum.COUNTY_IS_EXIST.getMessage(), JSONUtil.toJsonStr(repCountyList)));
            }

            CostWeightVO costWeightVO = costBaseVO.getCostWeightVO();
            CostWeightDO costWeightDO=new CostWeightDO();
            BeanUtils.copyProperties(costWeightVO,costWeightDO);
            costWeightDO.setGmtModified(date);
            costWeightDO.setGmtModifiedPin(costBaseVO.getGmtModifiedPin());
            costWeightDao.update(costWeightDO);//重量模板信息

            List<CostWeightRecordVO> costWeightRecordVOList = costWeightVO.getCostWeightRecordVOList();
            List<CostWeightRecordVO> costWeightRecordVOS = costWeightRecordDao.selectByCode(costWeightVO.getWeightCode());
            costWeightRecordVOS.forEach(costWeightRecordVO ->{
                Optional<CostWeightRecordVO> costWeightRecordOptional = costWeightRecordVOList.stream().filter(item -> costWeightRecordVO.getWeightRecordCode().equals(item.getWeightRecordCode())).findFirst();
                if(!costWeightRecordOptional.isPresent()){//删除不存在的
                    costWeightRecordDao.deleteByPrimaryKey(costWeightRecordVO.getId());
                }
            });
            Map<String,CostWeightRecordDO> costWeightRecordMap=new HashMap<>();
            costWeightRecordVOList.forEach(costWeightRecordVO ->{
                if(ObjectUtil.isNull(costWeightRecordVO.getWeightRecordCode())){//编码不存在新增
                    CostWeightRecordDO costWeightRecordDO=new CostWeightRecordDO();//重量模板明细
                    BeanUtils.copyProperties(costWeightRecordVO,costWeightRecordDO);
                    String weightRecordCode= TmsSerializeConstant.COST_WEIGHT_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_WEIGHT_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                    costWeightRecordDO.setWeightRecordCode(weightRecordCode);
                    costWeightRecordDO.setWeightCode(costWeightVO.getWeightCode());
                    costWeightRecordDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                    costWeightRecordDO.setIp(costBaseVO.getIp());
                    costWeightRecordDao.insert(costWeightRecordDO);
                    costWeightRecordMap.put(costWeightRecordVO.getWeightRecordNo(),costWeightRecordDO);
                }else{
                    CostWeightRecordDO costWeightRecordDO=new CostWeightRecordDO();//重量模板明细
                    BeanUtils.copyProperties(costWeightRecordVO,costWeightRecordDO);
                    costWeightRecordDO.setGmtModified(date);
                    costWeightRecordDO.setGmtModifiedPin(costBaseVO.getGmtModifiedPin());
                    costWeightRecordDO.setWeightCode(costWeightVO.getWeightCode());
                    costWeightRecordDao.update(costWeightRecordDO);//重量模板信息
                    costWeightRecordMap.put(costWeightRecordVO.getWeightRecordNo(),costWeightRecordDO);
                }
            });


            costPriceRecordDao.deleteByCostCode(costInfoVO.getCostCode());//成本价格明细
            List<CostPriceRecordVO> costPriceRecordVOList = costBaseVO.getCostPriceRecordVOList();
            costPriceRecordVOList.forEach(costPriceRecordVO ->{
                CostPriceRecordDO costPriceRecordDO=new CostPriceRecordDO();
                String priceRecordCode= TmsSerializeConstant.COST_PRICE_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COST_PRICE_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
                costPriceRecordDO.setPrice(costPriceRecordVO.getPrice());
                CostPartDO costPartDO = costPartMap.get(costPriceRecordVO.getPartNo());
                if(ObjectUtil.isNotNull(costPartDO)){
                    costPriceRecordDO.setPartCode(costPartDO.getPartCode());
                    costPriceRecordDO.setPartName(costPartDO.getPartName());
                    costPriceRecordDO.setPartSchemeCode(costPartDO.getPartSchemeCode());
                }
                CostWeightRecordDO costWeightRecordDO = costWeightRecordMap.get(costPriceRecordVO.getWeightRecordNo());
                if(ObjectUtil.isNotNull(costWeightRecordDO)){
                    costPriceRecordDO.setWeightRecordCode(costWeightRecordDO.getWeightRecordCode());
                    costPriceRecordDO.setWeightRecordName(costWeightRecordDO.getWeightRecordName());
                    costPriceRecordDO.setWeightCode(costWeightRecordDO.getWeightCode());
                }
                costPriceRecordDO.setCostRecordCode(priceRecordCode);
                costPriceRecordDO.setCostCode(costInfoVO.getCostCode());
                costPriceRecordDO.setGmtCreatePin(costBaseVO.getGmtCreatePin());
                costPriceRecordDO.setIp(costBaseVO.getIp());
                costPriceRecordDao.insert(costPriceRecordDO);
            });
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
}
